package com.yaoran.userauth.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yaoran.userauth.dao.ApplicationDao;
import com.yaoran.userauth.entity.Application;
import com.yaoran.userauth.entity.User;
import com.yaoran.userauth.service.ApplicationService;
import com.yaoran.userauth.utils.PageUtils;
import com.yaoran.userauth.utils.Query;
import com.yaoran.userauth.utils.ThreadLocalUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApplicationServiceImpl implements ApplicationService {
    private final static Logger logger = LoggerFactory.getLogger(ApplicationServiceImpl.class);
    @Autowired
    private ApplicationDao applicationDao;

    @Override
    public void addApplication(Application application, Date createTime){
        if (createTime != null){
            application.setCreateTime(createTime);
        }else {
            application.setCreateTime(new Date());
        }
        application.setUpdateTime(new Date());
        String name = ThreadLocalUtils.get("operate");
        application.setCreateBy(name);
        applicationDao.addApplication(application);
    }

    @Override
    public PageUtils<Application> listApplication(Query query,String keyWord){
        Page<Application> page = PageHelper.startPage(query.getPage(),query.getLimit());
        List<Application> list = applicationDao.listApplication(keyWord);
        int total = (int) page.getTotal();
        return new PageUtils(list,total,query.getLimit(),query.getPage());
    }

    @Override
    public void updateApplication(Application application){
        application.setUpdateTime(new Date());
        String name = ThreadLocalUtils.get("operate");
        application.setUpdateBy(name);
        applicationDao.updateApplication(application);
    }

    @Override
    public void deleteApplication(Long id){
        applicationDao.deleteApplication(id);
    }

    @Override
    public void deleteManyApplication(Long[] ids){
        applicationDao.deleteManyApplications(ids);
    }

    @Override
    public Application getApplication(Long id){
        return applicationDao.getApplication(id);
    }

    @Override
    public List<Application> listAllApplication(Long organizationId){
        return applicationDao.listAllApplication(organizationId);
    }

}
